package com.youxue.teachin.user.newuser;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import com.Navigator;
import com.youxue.teachin.BuildConfig;
import com.youxue.teachin.R;
import com.youxue.teachin.common.utils.SchemaParam;
import com.youxue.teachin.user.newuser.presenter.NewUserPresenter;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-04 21:07
 * @version: 9.1.0
 */
public class NewUserActivity extends Activity implements OnClickListener, OnEditorActionListener, INewUserView {
    private EditText mLoginInputPhone;
    private EditText mLoginPassword;
    private ImageView mShowPassword;
    private EditText mLoginInputCaptcha;
    private TextView mGetCaptcha;
    private TextView mRegistBt;
    private TextView mUserAgreement;
    private TextView mNewUser;
    private TextView mLookAround;

    NewUserPresenter mPresenter;

    private String mchaptch;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.user_new_user_activity);
        initView();
        mPresenter = new NewUserPresenter(this);
    }

    private void initView() {
        mLoginInputPhone = (EditText)findViewById(R.id.login_input_phone);
        mLoginPassword = (EditText)findViewById(R.id.login_password);
        mShowPassword = (ImageView)findViewById(R.id.show_password);
        mLoginInputCaptcha = (EditText)findViewById(R.id.login_input_captcha);
        mGetCaptcha = (TextView)findViewById(R.id.get_captcha);
        mRegistBt = (TextView)findViewById(R.id.regist_bt);
        mUserAgreement = (TextView)findViewById(R.id.user_agreement);
        mNewUser = (TextView)findViewById(R.id.new_user);
        mLookAround = (TextView)findViewById(R.id.look_around);

        mLoginInputCaptcha.setImeOptions(EditorInfo.IME_ACTION_SEND);
        mLoginInputPhone.setOnEditorActionListener(this);
        mLoginPassword.setOnEditorActionListener(this);
        mLoginInputCaptcha.setOnEditorActionListener(this);
        mShowPassword.setVisibility(View.VISIBLE);

        mGetCaptcha.setOnClickListener(this);
        mRegistBt.setOnClickListener(this);
        mNewUser.setOnClickListener(this);
        mShowPassword.setOnClickListener(this);
        findViewById(R.id.user_agreement_layout).setOnClickListener(this);

        if (BuildConfig.DEBUG) {
            mLoginInputPhone.setText("18501714348");
        }
    }

    boolean mPasswordVisible = false;

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.get_captcha) {
            mPresenter.getCaptcha();
        } else if (id == R.id.regist_bt) {
            mPresenter.createNewUser();
        } else if (id == R.id.new_user) {
            new Navigator.Builder()
                .setPageName("login")
                .addParameter(SchemaParam.TOLAUNCHE, true)
                .build().open();
            finish();
        } else if (id == R.id.show_password) {
            if (mPasswordVisible) {
                mLoginPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mShowPassword.setImageResource(R.drawable.password_visible);
            } else {
                mLoginPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mShowPassword.setImageResource(R.drawable.password_not_visible);
            }
            mPasswordVisible = !mPasswordVisible;

        } else if (id == R.id.user_agreement_layout) {
            //todo 用户协议
        } else if (id == R.id.look_around) {
            //todo 随便看看
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        int id = v.getId();
        if (id == R.id.login_input_phone) {
            mLoginPassword.requestFocus();
        } else if (id == R.id.login_password) {
            mLoginInputCaptcha.requestFocus();
        } else if (id == R.id.login_input_captcha) {
            mPresenter.createNewUser();
        } else {
            return false;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    @Override
    public String getPhone() {
        return mLoginInputPhone.getText().toString();
    }

    @Override
    public String getPassword() {
        return mLoginPassword.getText().toString();
    }

    @Override
    public String getCaptach() {
        return mLoginInputCaptcha.getText().toString();
    }

    @Override
    public void setServiceCaptch(String captch) {
        mchaptch = captch;
    }
}
